package chapter4.section5;

/**
 * 程序4-6：折半查找
 *
 * @author 作者姓名
 */
public class BinarySearchApp {
    public static void main(String[] args) {
        int mid, low, high;
        int[] arr = {8, 13, 21, 28, 35, 41, 52, 63, 71, 76, 81, 95, 101, 150, 164};
        low = 0;                            // 下界
        high = arr.length - 1;              // 上界
//        int x = Integer.parseInt(args[0]);  // x为要查找的元素
        int x = 35;
        do {
            mid = (low + high) / 2;
            if (x == arr[mid]) {
                System.out.println(x + "查找成功，在" + mid + "号位置");
                return;
            } else if (x < arr[mid])        // 如果x小于中间值则在左半段查找
                high = mid - 1;
            else if (x > arr[mid])          // 如果x大于中间值则在右半段查找
                low = mid + 1;
        } while (low <= high);
        System.out.println(x + "查找失败");
    }
}
